package iut.acy.cut4u.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Planning {

	private static Planning instance;
	private  ArrayList<Employe> lesEmployes;
	private  ArrayList<HoraireSalon> lesHoraires;
	private  ArrayList<CongeAnnuel> lesCongesAnnuels;
	private Semaine semaineEnCours;
	
	public  static  Planning getInstance() 
	{
		if (instance == null) 
			instance = new Planning(null,null,null,null);
		return instance;
	}
					
	public  static  Planning getInstance(ArrayList<Employe> lesEmployes,
			ArrayList<HoraireSalon> lesHoraires,
			ArrayList<CongeAnnuel> lesCongesAnnuels,
			Semaine semaineEnCours ) 
	{
		if (instance == null) 
			instance = new Planning(lesEmployes,
					 lesHoraires,
					 lesCongesAnnuels,
					 semaineEnCours);
		return instance;
	}

	public Planning(ArrayList<Employe> lesEmployes,
			ArrayList<HoraireSalon> lesHoraires,
			ArrayList<CongeAnnuel> lesCongesAnnuels,
			Semaine semaineEnCours) {
		super();
		this.lesEmployes = lesEmployes;
		this.lesHoraires = lesHoraires;
		this.lesCongesAnnuels = lesCongesAnnuels;
		this.semaineEnCours = semaineEnCours;
	}
	
	public boolean enregistreRendezVous (RendezVous rv)
	{ if (this.estCompatiblePlanning(rv))
			{if (this.semaineEnCours.getLesRendezVous().add(rv))
				return true;
			}
	  return false;
	}
	private	boolean estCompatibleCongesAnnuels (RendezVous rv)
	{
		GregorianCalendar rvDate = rv.getDate();
		for(CongeAnnuel ca : this.getLesFermeturesAnnuelles())
		{
			if(rvDate.after(ca.getDateDeb()) && rvDate.before(ca.getDateDeb()))
				return false;
			if(rvDate.equals(ca.getDateDeb()) || rvDate.equals(ca.getDateFin()))
				return false;
		}
		return false;
	}
	
	public  boolean estCompatibleHorairesOuvertures (RendezVous rv)
        {
                boolean compatible=true;
                int jour = rv.getDate().get(Calendar.DAY_OF_WEEK);
                if (jour == Calendar.SUNDAY)
                        jour =7;
                else
                        jour --;
                for (HoraireSalon h : lesHoraires)
                {
                if (h.getJour()==jour)
                        {
                        if  (rv.getHeureDebut().compareTo(new Horaire(12,0))<=0)
                                {
                                if (rv.getHeureDebut().compareTo(h.getHeureDebMatin())<0)
                                        compatible=compatible&false;
                                }
                        if  (rv.getHeureDebut().compareTo(new Horaire(12,0))>0)
                                {
                                if (rv.getHeureDebut().compareTo(h.getHeureDebAM())<0)
                                compatible=compatible&false;
                                }
                        }
                }
                return compatible;
        }
	/**
	 * @return the lesFermeturesAnnuelles
	 */
	public ArrayList<CongeAnnuel> getLesFermeturesAnnuelles() {
		return lesCongesAnnuels;
	}
	/**
	 * @return the lesHorairesOuvertures
	 */
	public ArrayList<HoraireSalon> getLesHorairesOuvertures() {
		return lesHoraires;
	}
	/**
	 * @param lesFermeturesAnnuelles the lesFermeturesAnnuelles to set
	 */
	public void setLesFermeturesAnnuelles(
			ArrayList<CongeAnnuel> lesFermeturesAnnuelles) {
		this.lesCongesAnnuels = lesFermeturesAnnuelles;
	}
	/**
	 * @param lesHorairesOuvertures the lesHorairesOuvertures to set
	 */
	public void setLesHorairesOuvertures(
			ArrayList<HoraireSalon> lesHorairesOuvertures) {
		this.lesHoraires = lesHorairesOuvertures;
	}
	
	public void changeSemaine ( int numeroSemaine){}
	public void semaineSuivante (){}
	public void semainePrecedente (){}
	
	public boolean estCompatibleAutresRendezVous (RendezVous rv)
        {
                boolean compatible=true;
                int jour = rv.getDate().get(Calendar.DAY_OF_WEEK);
               
                for (RendezVous r : this.semaineEnCours.getLesRendezVous())
                {
                        int jourLesRendezVous = r.getDate().get(Calendar.DAY_OF_WEEK);
                        if (((jour==jourLesRendezVous)&&(rv.getEmploye().getId()==r.getEmploye().getId()))||((jour==jourLesRendezVous)&&(rv.getClient().getId()==r.getClient().getId())))
                        {
                        if  ((rv.getHeureDebut().compareTo(r.getHeureDebut())>=0)&&(rv.getHeureDebut().compareTo(r.getHeureFin())<0))
                                compatible=compatible&false;
                        if  ((rv.getHeureFin().compareTo(r.getHeureDebut())>0)&&(rv.getHeureFin().compareTo(r.getHeureFin())<=0))
                                compatible=compatible&false;
                        }
                }
                return compatible;
        }
	
	
	public boolean estCompatiblePlanning (RendezVous rv)
	{
		return (this.estCompatibleAutresRendezVous(rv) && this.estCompatibleCongesAnnuels(rv) && this.estCompatibleHorairesOuvertures(rv));
	}
	

}
